import { Toast } from 'qmp-component'
import { reactive, computed } from 'vue'
import { queryAttachmentList } from '@/apis/common'
import { saveShopInfo } from '@/apis/ctmCustomerChange/index'
const none: any[] = [null, undefined, '']
interface anyObjectModle {
  [key: string]: any
}
/**
 * 查询附件
 * @param {string} modelId 对象ID
 * @param {Array} excludeTypeCodes 需排除的类型编码集合
 */
export const getAttachmentList = async (modelId: any, excludeTypeCodes: any[]) => {
  const list = await queryAttachmentList(modelId)
  return list.filter((v: any) => !excludeTypeCodes.includes(v.attachmentTypeCode))
}

// 清空省市区数据
function clearCityData() {
  return {
    provinceCode: '',
    provinceName: '',
    province: '',
    cityCode: '',
    cityName: '',
    city: '',
    countyCode: '',
    countyName: '',
    county: '',
    townCode: '',
    townName: '',
    town: ''
  }
}

/**
 * 创建门店
 */
export function createShopMixin(callback: Function) {
  const datas = reactive<anyObjectModle>({
    id: '',
    detail: {},
    formData: {
      id: '',
      name: '',
      businessType: [],
      businessTypeName: [],

      shopClassify: '',
      shopClassifyName: '',
      countryCode: '',
      countryName: '',
      shopAddr: '',
      floor: '',
      cityLevel: '',
      cityLevelName: '',
      marketLevelCode: '',
      marketLevelName: '',

      shopInfoPhone: '',
      shopServPhone: '',
      outpicArea: '',
      isDecorationDoorHead: false,
      doorHeadArea: ''
    },
    licenseList: [],
    agreementList: [],
    otherFiles: []
  })
  const shopDetail = computed(() => {
    return datas.detail?.shopDTO || {}
  })

  const onSave = () => {
    let flag = false
    let data = { ...datas.formData }
    data.businessType = datas.formData.businessType.join(',')
    data.businessTypeName = datas.formData.businessTypeName.join(',')
    data.isDecorationDoorHead = datas.formData.isDecorationDoorHead ? 1 : 0
    const requiredKeys = [
      'businessType',
      'name',
      'shopClassify',
      'countryCode',
      'shopAddr',
      'floor',
      'cityLevel',
      'marketLevelCode',
      'shopInfoPhone',
      'shopServPhone',
      'outpicArea',
      'isDecorationDoorHead'
    ]
    for (const [k, val] of Object.entries(data)) {
      if (requiredKeys.includes(k) && none.includes(val)) {
        flag = true
      }
    }
    if (data.countryCode === 'CN') {
      if (none.includes(data.county)) {
        flag = true
      }
    } else {
      data = { ...data, ...clearCityData() }
    }

    if (datas.licenseList.length === 0 || datas.agreementList.length === 0) {
      flag = true
    }
    if (flag) {
      Toast('请检查必填项是否填写完整')
      return
    }

    const attList = [...datas.licenseList, ...datas.agreementList]
    data.attachmentDTOS = [...datas.otherFiles, ...attList]
    data.changeCode = datas.detail.code
    if (shopDetail.value && shopDetail.value.code) {
      data = { ...shopDetail.value, ...data }
    }

    const regArea = /^[1-9][0-9]*([\.][0-9]{1,2})?$/
    if (data.outpicArea && !regArea.test(data.outpicArea)) {
      Toast('出图总面积输入不合法')
      return
    }
    if (data.isDecorationDoorHead) {
      if (none.includes(data.doorHeadArea)) {
        Toast('门头面积不能为空')
        return
      }
      if (data.doorHeadArea && !regArea.test(data.doorHeadArea)) {
        Toast('门头面积输入不合法')
        return
      }
    } else {
      // 非是否装修门头 则不传门头面积参数
      delete data.doorHeadArea
    }

    saveShopInfo(data).then((res: any) => {
      if (res.success === 1) {
        Toast('保存成功')
        callback && callback()
      }
    })
  }

  return {
    datas,
    onSave,
    shopDetail
  }
}

/**
 * 门店报图处理
 */
export function shopReportPicMixin(callback: Function) {
  const datas = reactive<anyObjectModle>({
    id: '',
    detail: {},
    formData: {
      isReportPic: ''
    },
    otherFiles: [],
    attrFileList: []
  })
  const shopDetail = computed(() => {
    return datas.detail?.shopDTO || {}
  })

  const onSave = () => {
    const { formData } = datas
    const data = {
      ...shopDetail.value,
      ...{
        isReportPic: formData.isReportPic // ? 1 : 0
      }
    }
    const attrFileList = datas.attrFileList || []

    if (none.includes(data.isReportPic)) {
      Toast('是否报图不能为空')
      return
    }
    // if (data.isReportPic === 0 && attrFileList.length === 0) {
    //   Toast('请上传特殊建档附件')
    //   return
    // }
    data.attachmentDTOS = [...datas.otherFiles, ...attrFileList]

    saveShopInfo(data).then((res: any) => {
      if (res.success === 1) {
        Toast('保存成功')
        callback && callback()
      }
    })
  }

  return {
    datas,
    shopDetail,
    onSave
  }
}
